package com.yc.ui;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;

import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.yc.Idao.LoginIDao;
import com.yc.dao.LoginDao;
import com.yc.ui.dialog.Dialog_Calendar;
import com.yc.ui.dialog.MoneyDialog;
import com.yc.utils.SWTUtil;
import com.yc.utils.StringUtil;

import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

public class Order extends Composite {
	private static Text text_1;
	private static Text text_2;
	private Table table;
	private static Text text_3;
	private static Text text_4;
	private static Text text_6;
	private static Text text;
	private static Text text_7;
	private Text text_9;
	private static Label lblNewLabel_9;
	
	private static double price; // 余额
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public Order(Composite parent, int style) {
		super(parent, style);

		setLayout(new FillLayout(SWT.HORIZONTAL));

		SashForm sashForm = new SashForm(this, SWT.VERTICAL);

		Composite composite = new Composite(sashForm, SWT.NONE);
		
		LoginIDao dao = new LoginDao();

		Composite composite_1 = new Composite(sashForm, SWT.NONE);
		composite_1.setLayout(new FillLayout(SWT.HORIZONTAL));

		SashForm sashForm_1 = new SashForm(composite_1, SWT.VERTICAL);

		Composite composite_2 = new Composite(sashForm_1, SWT.NONE);
		
		Label lblNewLabel_8 = new Label(composite_2, SWT.NONE);
		lblNewLabel_8.setBounds(197, 33, 173, 20);
		lblNewLabel_8.setText("通过房间号查询房间信息");

		text_9 = new Text(composite_2, SWT.BORDER);
		text_9.setBounds(437, 30, 138, 26);

		Composite composite_3 = new Composite(sashForm_1, SWT.NONE);
		composite_3.setBackgroundMode(SWT.INHERIT_DEFAULT);
		composite_3.setLayout(null);
		
		composite.setBackgroundImage( SWTResourceManager.getImage(Order.class, "/com/yc/images/客房预约1.PNG") );
		SWTUtil.imagetSize(composite);
		composite_2.setBackgroundImage( SWTResourceManager.getImage(Order.class, "/com/yc/images/客房预约2.PNG") );
		SWTUtil.imagetSize(composite_2);
		composite_3.setBackgroundImage( SWTResourceManager.getImage(Order.class, "/com/yc/images/客房预约3.PNG") );
		SWTUtil.imagetSize(composite_3);
		composite.setBackgroundMode(SWT.INHERIT_FORCE);
		composite_2.setBackgroundMode(SWT.INHERIT_FORCE);
		
		table = new Table(composite_3, SWT.BORDER | SWT.FULL_SELECTION);
		table.setBounds(106, 10, 828, 156);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		Button btnNewButton_1 = new Button(composite_2, SWT.NONE);
		btnNewButton_1.setImage(SWTResourceManager.getImage(Order.class, "/com/yc/images/客房预约按钮4.PNG"));
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String id = text_9.getText().trim();
				if(StringUtil.checkNull(id)) {
					return  ;
				}
				List<Map<String, Object>> list = dao.SelectRoom(id);
				showInfo(table, list);
			}
		});
		btnNewButton_1.setBounds(668, 28, 98, 30);
		btnNewButton_1.setText("查询");

		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(153);
		tblclmnNewColumn.setText("门牌号");

		TableColumn tblclmnNewColumn_1 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_1.setWidth(155);
		tblclmnNewColumn_1.setText("房间类型");

		TableColumn tblclmnNewColumn_2 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_2.setWidth(169);
		tblclmnNewColumn_2.setText("房间状态");

		TableColumn tblclmnNewColumn_4 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_4.setWidth(180);
		tblclmnNewColumn_4.setText("房间费用（/天）");

		TableColumn tblclmnNewColumn_5 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_5.setWidth(163);
		tblclmnNewColumn_5.setText("押金");
		sashForm_1.setWeights(new int[] {103, 182});
		Button btnHuoqu = new Button(composite, SWT.NONE);
		btnHuoqu.setImage(SWTResourceManager.getImage(Order.class, "/com/yc/images/客房预约按钮2.PNG"));
		//获取日历按钮
		btnHuoqu.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Dialog_Calendar dialog=new Dialog_Calendar(getShell(),SWT.DIALOG_TRIM|SWT.APPLICATION_MODAL);
				dialog.open();
			}
		});
		btnHuoqu.setBounds(344, 129, 103, 27);
		btnHuoqu.setText("获取日历");

		Label lblNewLabel = new Label(composite, SWT.NONE);
		lblNewLabel.setBounds(51, 36, 76, 20);
		lblNewLabel.setText("姓名：");
		
		text_7 = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		text_7.setBounds(712, 182, 180, 26);

		text_1 = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		text_1.setBounds(152, 33, 161, 26);

		Label lblNewLabel_1 = new Label(composite, SWT.NONE);
		lblNewLabel_1.setBounds(51, 81, 76, 20);
		lblNewLabel_1.setText("房间号：");

		text_2 = new Text(composite, SWT.BORDER);
		text_2.setBounds(152, 78, 161, 26);

		Button btnNewButton = new Button(composite, SWT.NONE);
		btnNewButton.setImage(SWTResourceManager.getImage(Order.class, "/com/yc/images/客房预约按钮1.PNG"));
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String r_id = text_2.getText().trim();
				String c_cardid = text_3.getText().trim();
				String start_date = text.getText().trim();
				String r_price = text_7.getText().trim();

				if(StringUtil.checkNull(r_id, c_cardid, start_date, r_price)) {
					StringUtil.showMessageBox(getShell(), "信息不完整，请完善信息后再预定");
					return;
				}

				int re = 0;
				re = dao.UpdateReserve(r_id, c_cardid, start_date, Double.parseDouble(r_price), price);
				if(re == 0) {
					StringUtil.showMessageBox(getShell(), "预定失败");
				}else if(re == -1) {
					StringUtil.showMessageBox(getShell(), "余额不足，为您跳转充值界面");
					MoneyDialog md = new MoneyDialog(getShell(), SWT.NONE, text_1.getText().trim());
					md.open();
				}else {
					int result = 0;
					result = dao.UpdateAlter(r_id, Double.parseDouble(r_price), c_cardid);
					if(result == -1) {
						StringUtil.showMessageBox(getShell(), "预定失败\n原因：付款失败");
						return ;
					}
					MainUI.getMoney();
					StringUtil.showMessageBox(getShell(), "预定成功");
					Room.Color(r_id);
					Login.Borider(c_cardid);
				}
			}
		});
		btnNewButton.setText("预定登记");
		btnNewButton.setBounds(117, 214, 103, 53);

		Label lblNewLabel_3 = new Label(composite, SWT.NONE);
		lblNewLabel_3.setBounds(593, 132, 83, 20);
		lblNewLabel_3.setText("房间价格：");

		Button btnNewButton_3 = new Button(composite, SWT.NONE);
		btnNewButton_3.setImage(SWTResourceManager.getImage(Order.class, "/com/yc/images/客房预约按钮3.PNG"));
		btnNewButton_3.setBounds(344, 214, 103, 53);
		btnNewButton_3.setText("打印订单");

		Label label = new Label(composite, SWT.NONE);
		label.setBounds(593, 36, 76, 20);
		label.setText("身份证号：");

		text_3 = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		text_3.setBounds(712, 33, 180, 26);


		Label lblNewLabel_5 = new Label(composite, SWT.NONE);
		lblNewLabel_5.setBounds(593, 81, 76, 20);
		lblNewLabel_5.setText("房间类型：");

		Label lblNewLabel_7 = new Label(composite, SWT.NONE);
		lblNewLabel_7.setBounds(51, 132, 76, 20);
		lblNewLabel_7.setText("预定日期：");

		text_4 = new Text(composite, SWT.BORDER);
		text_4.setBounds(712, 78, 180, 26);

		text_6 = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		text_6.setBounds(712, 129, 180, 26);


		text = new Text(composite, SWT.BORDER);
		text.setBounds(152, 129, 161, 26);

		Label lblNewLabel_4 = new Label(composite, SWT.NONE);
		lblNewLabel_4.setText("预付金额：");
		lblNewLabel_4.setBounds(593, 185, 76, 20);

		

		lblNewLabel_9 = new Label(composite, SWT.NONE);
		lblNewLabel_9.setBounds(152, 156, 161, 20);
		lblNewLabel_9.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		sashForm.setWeights(new int[] {287, 288});

	}

	public static void xuanran(Map<String, Object> map, Map<String, Object> roomMap) {
		price = Double.parseDouble(map.get("c_money").toString());
		text_1.setText(StringUtil.toString(map.get("c_name")));
		text_2.setText(StringUtil.toString(roomMap.get("r_id")));
		text_3.setText(StringUtil.toString(map.get("c_cardid")));
		text_4.setText(StringUtil.toString(roomMap.get("r_type")));
		text_6.setText(StringUtil.toString(roomMap.get("r_price")));
		text_7.setText(StringUtil.toString(Integer.parseInt(StringUtil.toString(roomMap.get("r_price"))) * 0.2));
	}

	public static void setDate(String str){
		lblNewLabel_9.setText("");
		text.setText(str); 
	}

	public static void getDate(String name){
		lblNewLabel_9.setText(name);
	}
	
	protected void showInfo(Table table, List<Map<String, Object>> list) {
		table.removeAll();
		TableItem tableItem = null;
		for(Map<String, Object> m : list) {
			tableItem = new TableItem(table, SWT.NONE);
			tableItem.setText(new String[] {
					m.get("r_id").toString(),
					m.get("r_type").toString(),
					m.get("r_status").toString(),
					m.get("r_price").toString(),
					String.valueOf((Double.parseDouble(m.get("r_price").toString()) * 0.2)),
			});
		}
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
